<?php

class BaseModel extends ActiveRecord\Model {

    function __set($name, $value) {
        $table = static::table();
        if (($table->has_relationship($name)) && ($class = get_class($value))) {
            $relation = $table->get_relationship($name);
            if ($class == $relation->class_name) {
                return $this->assign_attribute($relation->foreign_key[0], $value->id);
            } else {
                throw new RelationshipException();
            }
        }
        parent::__set($name, $value);
    }

    public static function switch_connection($name) {
        $cfg = ActiveRecord\Config::instance();
        $valid = $cfg->get_connections();
        if (!isset($valid[$name])) {
            throw new ActiveRecord\DatabaseException('Invalid connection specified');
        }

        // Get the name of the current connection
        $old = self::$connection;

        $cm = ActiveRecord\ConnectionManager::instance();
        $conn = $cm::get_connection($name);
        static::table()->conn = $conn;

        return $old;
    }

}

?>
